package com.my.study.springframework.bean.factory.support;

import com.my.study.springframework.bean.BeansException;
import com.my.study.springframework.bean.factory.FactoryBean;
import com.my.study.springframework.bean.factory.config.BeanDefinition;
import com.my.study.springframework.bean.factory.config.BeanPostProcessor;
import com.my.study.springframework.bean.factory.config.ConfigurableBeanFactory;
import com.my.study.springframework.util.ClassUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: yidujun
 * @create: 2021/06/10 13:44
 */
public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements ConfigurableBeanFactory {

    /** ClassLoader to resolve bean class names with, if necessary */
    private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();

    /** BeanPostProcessors to apply in createBean */
    private final List<BeanPostProcessor> beanPostProcessors = new ArrayList<>();

    @Override
    public Object getBean(String beanName) throws BeansException {
        return doGetBean(beanName, null);
    }

    @Override
    public Object getBean(String name, Object... args) throws BeansException {
        return doGetBean(name, args);
    }

    @Override
    public <T> T getBean(String name, Class<T> requiredType) {
        return (T) getBean(name);
    }

    protected <T> T doGetBean(final String beanName, final Object[] args) {
        Object sharedInstance  = getSingleton(beanName);
        if (sharedInstance  != null) {
            return (T) getObjectForBeanInstance(sharedInstance , beanName);
        }
        BeanDefinition beanDefinition = getBeanDefinition(beanName);
        Object bean = createBean(beanName, beanDefinition, args);
        return (T) getObjectForBeanInstance(bean, beanName);
    }

    /**
     * 根据beanName获取BeanDefinition信息
     * b
     * @param beanName beanName
     * @return
     * @throws BeansException
     */
    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;

    /**
     * 创建实例对象
     *
     * @param beanName beanName
     * @param beanDefinition
     * @param args 参数
     * @return
     * @throws BeansException
     */
    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException;

    @Override
    public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
        this.beanPostProcessors.remove(beanPostProcessor);
        this.beanPostProcessors.add(beanPostProcessor);
    }

    public List<BeanPostProcessor> getBeanPostProcessors() {
        return this.beanPostProcessors;
    }

    public ClassLoader getBeanClassLoader() {
        return this.beanClassLoader;
    }

    private Object getObjectForBeanInstance(Object beanInstance, String beanName) {
        if (!(beanInstance instanceof FactoryBean)) {
            return beanInstance;
        }

        Object obj = getCachedObjectForFactoryBean(beanName);

        if (null == obj) {
            FactoryBean<?> factoryBean = (FactoryBean<?>) beanInstance;
            obj = getObjectFromFactoryBean(factoryBean, beanName);
        }

        return obj;
    }
}
